<template>
	<view style="">
		<u-navbar :title="title" :bgColor="bgcolor" :autoBack="true" :placeholder="true">
		</u-navbar>
		<view class="note">

		</view>
		<view class="" style="padding: 15px;position: relative;z-index: 1;">
			<view style="">
				<view class="flex"
					style="padding: 25px 15px; background-color: #fff; border-radius: 10px;box-shadow: 0px 2 5px 0px rgba(0,0,0,0.05);">
					<view class="tit">业务员姓名</view>
					<u--input style="text-align: right;" placeholder="请输入内容" v-model="name" border="none"
						clearable></u--input>
				</view>
				<view class="flex"
					style="padding: 25px 15px; background-color: #fff; border-radius: 10px;box-shadow: 0px 2 5px 0px rgba(0,0,0,0.05);margin-top: 15px;">
					<view class="tit">业务员手机号</view>
					<u--input placeholder="请输入内容" v-model="phone" border="none" clearable></u--input>
				</view>
				<view v-if="cusme1 != ''" class="flex"
					style="padding: 25px 15px; background-color: #fff; border-radius: 10px;box-shadow: 0px 2 5px 0px rgba(0,0,0,0.05);margin-top: 15px;">
					<view class="tit">{{ cusme1 }}</view>
					<u--input placeholder="请输入内容" v-model="cusmeone" border="none" clearable></u--input>
				</view>
				<view v-if="cusme2 != ''" class="flex"
					style="padding: 25px 15px; background-color: #fff; border-radius: 10px;box-shadow: 0px 2 5px 0px rgba(0,0,0,0.05);margin-top: 15px;">
					<view class="tit">{{ cusme2 }}</view>
					<u--input placeholder="请输入内容" v-model="cusmetwo" border="none" clearable></u--input>
				</view>
				<view v-if="is_area == '1'" class="flex" style="padding: 25px 15px; background-color: #fff; border-radius: 10px;box-shadow: 0px 2 5px 0px rgba(0,0,0,0.05);margin-top: 15px;
				justify-content: space-between;">
					<view class="tit">作业省市地址</view>
					<view style="position: relative;">
						<u--input disabled suffixIcon="arrow-right" suffixIconStyle="color: #909399" placeholder="请输入内容"
							border="none" v-model="address" clearable></u--input>

						<view @click="addressShow = true" class=""
							style="position: absolute;top: 0;width: 100%;height: 100%;">

						</view>
					</view>
				</view>
				<view v-if="status == 3" @click="submit" class="flex" style="
						width: 255px;
						height: 40px;
						background: #2447FF;
						border-radius: 20px;
						font-size: 14px;
						color: #fff;
						margin: 30px auto 0;
						justify-content: center;
					">
					{{ tit }}
				</view>
			</view>
			<view v-if="codeflowpath.content"
				style="padding: 15px; background-color: #fff; border-radius: 10px; margin-top: 15px">
				<!-- <view style="font-size: 16px; font-weight: 400; color: #1d1f20">重要提示</view> -->
				<view class="" v-if="codeflowpath != null">
					<u-parse style="margin-top: 15px" :content="codeflowpath.content"></u-parse>
				</view>
			</view>
		</view>
		<address-picker :show="addressShow" closeOnClickOverlay @confirm="confirmAddress" @cancel="addressShow = false"
			@close="addressShow = false" :address-data="addressData" :indexs="indexs" :areaId="areaId"
			:type="type"></address-picker>
	</view>
</template>

<script>
	import config from '@/common/config/index.js';
	export default {
		data() {
			return {
				tit: '保存',
				content: `
							<p>露从今夜白，月是故乡明</p>
							<img src="https://cdn.uviewui.com/uview/swiper/2.jpg" />
								`,
				address: '',
				date: [],
				addressShow: false,
				indexs: [0, 0, 0],
				areaId: [1, 110000, 110101],
				addressData: ['北京市', '北京市', '东城区'],
				type: 3, //1-省，2-省市，3-省市区
				baseurl: config.prodBase,
				project_id: '',
				codeflowpath: '',
				is_area: '',
				name: '',
				phone: '',
				sign_id: '',
				status: 3,
				show_list: [],
				show_msg: [],
				cusme2: '',
				cusme1: '',
				cusmeone: '',
				cusmetwo: '',
				inSubmit: false,
				bgcolor: 'rgba(255,255,255,0)',
				title: '申请推广码'
			};
		},
		onPageScroll(e) {
			if (e.scrollTop > 50) {
				this.bgcolor = 'rgba(255,255,255,1)'
			} else {
				this.bgcolor = 'rgba(255,255,255,0)'
			}
		},
		onLoad(e) {
			this.type = e.type;
			this.project_id = e.project_id;
			this.sign_id = e.sign_id;
			if (e.type == 1) {
				uni.setNavigationBarTitle({
					title: '报备资料',
				});
				this.tit = '保存';
				// this.apply()
				this.update_info();
			}
			if (e.type == 2) {
				uni.setNavigationBarTitle({
					title: '申请推广码',
				});
				this.tit = '申请推广码';
				this.apply();
			}
		},
		methods: {
			async update_info() {
				let res = await this.$http.index.updateCode({
					sign_id: this.sign_id,
					project_id: this.project_id,
				});
				if (res.code == 1) {
					this.status = res.data.signInfo.status;
					this.name = res.data.signInfo.signname;
					this.phone = res.data.signInfo.signphone;
					this.is_area = res.data.is_area;
					this.codeflowpath = res.data.codeflowpath;
					console.log(this.codeflowpath);
					this.address =
						res.data.signInfo.province_id + '-' + res.data.signInfo.city_id + '-' + res.data.signInfo
						.area_id;
					this.cusme1 = res.data.custom.key1;
					this.cusme2 = res.data.custom.key2;
					if (this.cusme1 != '') {
						this.cusmeone = res.data.signInfo.custom1;
					}
					if (this.cusme2 != '') {
						this.cusmetwo = res.data.signInfo.custom2;
					}
				}
			},
			async submit() {
				if (this.inSubmit) return;
				this.inSubmit = true;

				if (this.type == 1) {
					if (this.name == '') {
						this.$tips('请输入姓名');
						return;
					}
					if (this.phone == '') {
						this.$tips('请输入手机号');
						return;
					}
					if (this.cusme1 != '') {
						if (this.cusmeone == '') {
							this.$tips('请填写' + this.cusme1);
							return;
						}
					}
					if (this.cusme2 != '') {
						if (this.cusmetwo == '') {
							this.$tips('请填写' + this.cusme2);
							return;
						}
					}
					if (this.is_area == 1) {
						if (this.address == '') {
							this.$tips('请选择地址');
							return;
						}
					}
					let res = await this.$http.index.updateCode_post({
						project_id: this.project_id,
						name: this.name,
						phone: this.phone,
						area: this.address,
						sign_id: this.sign_id,
						custom1: this.cusmeone,
						custom2: this.cusmetwo,
					});
					if (res.code == 1) {
						uni.showToast({
							icon: 'none',
							title: res.msg,
						});
						setTimeout(() => {
							uni.redirectTo({
								url: '/pages/project/detail?project_id=' + this.project_id + '&id=2',
							});
						}, 1000);
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg,
						});
					}
				} else {
					if (this.name == '') {
						this.$tips('请输入姓名');
						return;
					}
					if (this.phone == '') {
						this.$tips('请输入手机号');
						return;
					}
					if (this.cusme1 != '') {
						if (this.cusmeone == '') {
							this.$tips('请填写' + this.cusme1);
							return;
						}
					}
					if (this.cusme2 != '') {
						if (this.cusmetwo == '') {
							this.$tips('请填写' + this.cusme2);
							return;
						}
					}
					if (this.is_area == 1) {
						if (this.address == '') {
							this.$tips('请选择地址');
							return;
						}
					}
					let res = await this.$http.car.applyCode({
						project_id: this.project_id,
						name: this.name,
						phone: this.phone,
						area: this.address,
						custom1: this.cusmeone,
						custom2: this.cusmetwo,
					});
					console.log(res);
					if (res.code == 1) {
						uni.showToast({
							icon: 'none',
							title: '申请成功',
						});
						setTimeout(() => {
							uni.redirectTo({
								url: '/pages/project/detail?project_id=' + this.project_id + '&id=2',
							});
						}, 800);
					} else {
						this.$tips(res.msg);
					}
				}

				this.inSubmit = false;
			},
			apply() {
				let that = this;
				uni.request({
					url: that.baseurl + 'project/applyCode?project_id=' + that.project_id,
					method: 'GET',
					header: {
						token: uni.getStorageSync('token'),
					},
					success(res) {
						console.log(res);
						if (res.data.code == 1) {
							that.cusme1 = res.data.data.custom.key1;
							that.cusme2 = res.data.data.custom.key2;
							that.show_list = res.data.data.custom;
							that.codeflowpath = res.data.data.codeflowpath;
							that.is_area = res.data.data.is_area;
						}
					},
				});
			},
			opd() {},
			confirmAddress(val) {
				this.address = val.value[0] + '-' + val.value[1] + '-' + val.value[2];
				this.addressShow = false;
			},
		},
	};
</script>

<style>
	page {
		background-color: #f7f9fa;
	}
</style>
<style scoped lang="scss">
	.tit {
		font-size: 14px;
		font-weight: 400;
		color: #898b8b;
	}

	/deep/.uni-input-wrapper {
		text-align: right;
	}

	/deep/ .u-input {
		text-align: right;
		// padding: 5px 0px 15px !important;
		background-color: transparent !important;
	}

	.note {
		background-image: url('@/static/font/21.png');
		position: fixed;
		width: 100%;
		height: 100%;
		background-size: 100% 100%;
		top: 0;
	}

	.flex {
		display: flex;
		align-items: center;
	}
</style>